//
//  HJPersonalCenterController.m
//  KaiChat
//
//  Created by lx on 2023/10/18.
//

#import "HJPersonalCenterController.h"
#import "HJBackgroundView.h"
#import "HJIconTitleCell.h"
#import "HJUIAlertController.h"
#import "HJMinePresent.h"

@interface HJPersonalCenterController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) UIView *maskView;
@property (nonatomic, assign) CGFloat margin;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSArray *datas;


@end

@implementation HJPersonalCenterController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _margin = KScreenWidth / 5;
    
    UIButton *maskView = [[UIButton alloc] initWithFrame:self.view.bounds].addTo(self.view);
    maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    _maskView = maskView;
    [maskView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    
    HJBackgroundView *contentView = [HJBackgroundView addToView:maskView];
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.image = [UIImage imageNamed:@"info_top"];
    contentView.frame = CGRectMake(-KScreenWidth, 0, KScreenWidth - _margin, KScreenHeight);
    _contentView = contentView;
    
    
    UIView *contentBackgroundView = [HJBackgroundView addToView:contentView];
    contentBackgroundView.backgroundColor = [UIColor.whiteColor colorWithAlphaComponent:1];
    contentBackgroundView.tj_size = CGSizeMake(contentView.tj_width, KScreenHeight - 122);
    [contentBackgroundView addRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight radius:10];
    [contentBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(122);
        make.bottom.offset(0);
    }];
    
    [contentView addSubview:self.tableView];
    [self.tableView registerClass:HJIconTitleCell.class forCellReuseIdentifier:@"HJIconTextCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = cellHeight;
    self.tableView.backgroundColor = UIColor.clearColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.bounces = NO;
    self.tableView.scrollEnabled = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(contentBackgroundView);
        make.bottom.offset(0);
    }];
    
    HJStyle2Button *logoutButton = [HJStyle2Button buttonWithSuperView:contentView font:UIFontMediumMake(14) color:nil title:@"log out"];
    [logoutButton addTarget:self action:@selector(didClickLogoutButton) forControlEvents:UIControlEventTouchUpInside];
    [logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(20);
        make.height.offset(55);
        make.bottom.inset(30 + TabbarSafeBottomMargin);
    }];
    
    UIImageView *avatarImageView = [UIImageView addToView:contentView];
    avatarImageView.addCornerRadius(28);
    [avatarImageView tj_setImageWithURL:HJUserInfo.avatar.URL placeholderImage:[UIImage imageNamed:@"avatar"]];
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.width.height.offset(54);
        make.top.offset(KStatusBarHight);
    }];
    
    UILabel *nicknameLabel = [UILabel lableWithSuperView:contentView font:UIFontMediumMake(16) color:UIColor.tj_333333Color title:HJUserInfo.nickname textAlignment:NSTextAlignmentLeft];
    [nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(avatarImageView).offset(4);
        make.left.equalTo(avatarImageView.mas_right).offset(10);
    }];

    UILabel *userIdLabel = [UILabel lableWithSuperView:contentView font:UIFontMake(12) color:UIColor.tj_868391Color title:HJUserInfo.no textAlignment:NSTextAlignmentLeft];
    [userIdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nicknameLabel);
        make.top.equalTo(nicknameLabel.mas_bottom).offset(4);
    }];
    
}

- (void)didClickLogoutButton {
    HJUIAlertController *alert = [HJUIAlertController alertControllerWithTitle:@"Confirm to quit the account?" message:@"Confirm to quit the account?" preferredStyle:HJUIAlertControllerStyleActionSheet];
    [alert addAction:[HJUIAlertAction actionWithTitle:@"OK" style:HJUIAlertActionStyleDestructive handler:^(HJUIAlertAction * _Nonnull action) {
        [HJMinePresent logoutComplete:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:logoutNotification object:nil];
    }]];
    [alert addAction:[HJUIAlertAction actionWithTitle:@"Cancel" style:HJUIAlertActionStyleCancel handler:^(HJUIAlertAction * _Nonnull action) {
        
    }]];
    
    [alert showInViewController:self];
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJIconTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJIconTextCell" forIndexPath:indexPath];
    HJCellModel *model = self.datas[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HJCellModel *model = self.datas[indexPath.row];
    NSString *controllerName = model.controllerName;
    if (isEmptyString(controllerName)) {
        return;
    }
    
    [self dismissComplete:^{
        [HJTools.currentNav pushViewControllerWithName:controllerName];
    }];
    
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationCustom;
}
 

- (void)dismiss {
    [self dismissComplete:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.tj_x = 0;
        self.maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    } completion:^(BOOL finished) {
    }];
}

- (void)dismissComplete:(void (^)(void))complete {
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_x = -KScreenWidth;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (complete) {
                complete();
            }
        }];
    }];
}


- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        tableView.separatorColor = [UIColor tj_separatorColor];
        tableView.backgroundColor = [UIColor tj_backgroundColor];
        tableView.tableFooterView = [[UIView alloc] init];
        tableView.showsHorizontalScrollIndicator = NO;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedRowHeight = 0;
        
        if (@available(iOS 15.0, *)) {
            tableView.sectionHeaderTopPadding = 0;
        }
        
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
}

- (NSArray *)datas {
    if (!_datas) {
        _datas = @[
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"about";
                model.identifier = @"about";
                model.text = @"About Kai";
                model.controllerName = @"HJAboutController";
                model.showLine = NO;
            }],
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"cancellation";
                model.text = @"Account Cancellation";
                model.identifier = @"cancellation";
                model.controllerName = @"HJCancellationController";
                model.showLine = NO;
            }],
        ];
        
        
    }
    return _datas;
}

@end
